<#
.SYNOPSIS
Start and verify Student Management System

.DESCRIPTION
This script starts the Student Management System using Docker Compose and verifies basic functionality.

.NOTES
Requires Docker and Docker Compose to be installed
#>

# Ensure running as Administrator
if (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]"Administrator"))
{
    Write-Host "Please run this script as Administrator!"
    Exit
}

# Check if Docker is installed
Write-Host "Checking if Docker is installed..."
if (-not (Get-Command "docker" -ErrorAction SilentlyContinue))
{
    Write-Host "Docker not found. Please install Docker Desktop for Windows first."
    Exit
}

# Check if Docker is running
Write-Host "Checking if Docker is running..."
try {
    docker info > $null
} catch {
    Write-Host "Docker is not running. Please start Docker Desktop first."
    Exit
}

# Define variables
$dockerComposeFile = Join-Path -Path $PSScriptRoot -ChildPath "docker\docker-compose.yml"
$envFile = Join-Path -Path $PSScriptRoot -ChildPath "docker\.env"

# Check if .env file exists, create if not
if (-not (Test-Path $envFile)) {
    Write-Host ".env file not found, creating..."
    Copy-Item "$PSScriptRoot\docker\.env.example" $envFile
    Write-Host "Created .env file. You may edit it if needed."
}

# Start Docker Compose services
Write-Host "Starting Student Management System services..."
docker-compose -f $dockerComposeFile --env-file $envFile up -d

if ($LASTEXITCODE -ne 0) {
    Write-Host "Docker Compose failed to start. Please check error messages."
    Exit
}

# Wait for services to start
Write-Host "Waiting for services to start..."
Start-Sleep -Seconds 30

# Verify MySQL database
Write-Host "Verifying MySQL database..."
docker exec -it student-management-db mysql -u admin -p123456 -e "USE StudentManagementDB; SHOW TABLES;" StudentManagementDB

if ($LASTEXITCODE -ne 0) {
    Write-Host "MySQL database verification failed. Services may not be fully started yet."
} else {
    Write-Host "MySQL database verification successful!"
}

# Verify backend API
Write-Host "Verifying backend API..."
try {
    $apiUrl = "http://localhost:5000/swagger/index.html"
    $response = Invoke-WebRequest -Uri $apiUrl -UseBasicParsing -TimeoutSec 10
    Write-Host "Backend API verification successful! Access API docs at: $apiUrl"
} catch {
    Write-Host "Backend API verification failed. Services may not be fully started yet."
}

# Verify frontend application
Write-Host "Verifying frontend application..."
try {
    $frontendUrl = "http://localhost:80"
    $response = Invoke-WebRequest -Uri $frontendUrl -UseBasicParsing -TimeoutSec 10
    Write-Host "Frontend application verification successful! Access frontend at: $frontendUrl"
} catch {
    Write-Host "Frontend application verification failed. Services may not be fully started yet."
}

# Display login information
Write-Host ""
Write-Host "System has started. You can login with these default accounts:"
Write-Host "- Admin account: admin / Admin@123"
Write-Host "- Teacher account: teacher / Teacher@123"
Write-Host "- Student account: student / Student@123"

Write-Host ""
Write-Host "To stop the system, run:"
Write-Host "docker-compose -f $dockerComposeFile --env-file $envFile down"